package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.rare;

import net.minecraft.entity.Entity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.Box;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;
import net.xxcxcxcx.xc_enhancedmob.skills.SkillRarity;

import java.util.Iterator;
import java.util.List;

public class RadiateSkill extends BaseSkill {
    public RadiateSkill(int level) {
        super(level);
    }

    public RadiateSkill(NbtCompound nbtCompound) {
        super(nbtCompound);
    }

    public RadiateSkill() {
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setCooldown(50);
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return "每个一小段时间内一定范围内的所有实体受到一次伤害。";
    }

    public SkillRarity getSkillRarity() {
        return SkillRarity.Rare;
    }

    @Override
    public boolean canUseAbility(MobEntity mobEntity) {
        return (mobEntity.world.getTime() > getSkilltime());
    }

    @Override
    public boolean passiveApply(MobEntity mobEntity) {
        if (canUseAbility(mobEntity)) {
            setSkilltime(mobEntity.world.getTime() + getCooldown() - getSkillLevel());
            double Range = getSkillLevel() * 2.0f + 5.0f;
            double L = Range * 2;
            Box Box = mobEntity.getBoundingBox().stretch(L, L / 2, L).stretch(-L, -L / 2, -L);
            List<Entity> entityList = mobEntity.world.getOtherEntities(mobEntity, Box);
            Iterator iterator1 = entityList.iterator();
            while (iterator1.hasNext()){
                Entity target = (Entity) iterator1.next();
                double X = mobEntity.getX() - target.getX();
                double Y = mobEntity.getY() - target.getY();
                double Z = mobEntity.getZ() - target.getZ();
                if ((X * X + Y * Y + Z * Z) < Range * Range) {
                    if(!(target instanceof ServerPlayerEntity playerEntity && playerEntity.abilities.creativeMode))
                        target.damage(DamageSource.OUT_OF_WORLD, getSkillLevel()+1.0f);
                }
            }
        }
        return true;
    }
}
